package cn.skyclass.controls
{
	import flash.events.Event;
	import flash.events.FocusEvent;
	
	import mx.controls.TextInput;
	
	[Bindable]
	public class PromptTextInput extends TextInput
	{
		public function PromptTextInput()
		{
			this.addEventListener(FocusEvent.FOCUS_IN, onFocusIn);
			this.addEventListener(FocusEvent.FOCUS_OUT, onFocusOut);
		}


		private var _promptEnabled:Boolean = true;
		public function get promptEnabled():Boolean{
			return this._promptEnabled;
		}
		public function set promptEnabled(value:Boolean):void{
			this._promptEnabled = value;
			
			// enabled, set prompt info
			if(this._promptEnabled && this.prompt && !super.text){
				super.text = this.fullPromptText;
			}
			
			// disabled, restore to empty
			if(!this._promptEnabled && (super.text == this.fullPromptText)){
				super.text = "";
			}
		}
		
		private var _promptOpen:String = "<";
		public function get promptOpen():String{
			return this._promptOpen;
		}
		public function set promptOpen(value:String):void{
			this._promptOpen = value;
			this.updateFullPromptText();
		}
		
		
		private var _promptClose:String = ">";
		public function get promptClose():String{
			return this._promptClose;
		}
		public function set promptClose(value:String):void{
			this._promptClose = value;
			this.updateFullPromptText();
		}
		
		
		private var _prompt:String = "";
		public function get prompt():String{
			return this._prompt;
		}
		public function set prompt(value:String):void{
			this._prompt = value;
			this.updateFullPromptText();
		}
		
		
		private var _fullPromptText:String = "";
		protected function get fullPromptText():String{
			return this._fullPromptText;
		}
		protected function set fullPromptText(value:String):void{
			this._fullPromptText = value;
		}
		
		
		protected function updateFullPromptText():void{
			var old:String = this.fullPromptText;
			
			this.fullPromptText = this.promptOpen + this.prompt + this.promptClose;
			if(this.promptEnabled && this.prompt && (!super.text || super.text == old)){
				super.text = this.fullPromptText;
			}
		}
		
		protected function onFocusIn(e:Event):void{
			if(this.promptEnabled && (super.text == this.fullPromptText)){
				super.text = "";
			}
		}
		
		protected function onFocusOut(e:Event):void{
			if(this.promptEnabled && this.prompt && !super.text){
				super.text = this.fullPromptText;
			}
		}
		
		
		override public function get text():String{
			if(this.promptEnabled && (super.text == this.fullPromptText)){
				return "";
			}else{
				return super.text;
			}
		}
		override public function set text(value:String):void{
			if(this.promptEnabled && !value){
				super.text = this.fullPromptText;
			}else{
				super.text = value;
			}
		}
		
	}
}